#!/bin/sh
# Copyright 2007-2009 Dylan Cochran
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# $Id: run 774 2009-02-26 16:50:23Z heliocentric $

export ROOTDIR=`pwd`

export PATH="${PATH}:${ROOTDIR}/share/bin"

if [ ! -c "${DEVICES}/null" ] ; then
	if [ -c "/dev/null" ] ; then
		DEVICES=/dev
	else
		echo "Error, no device nodes found"
		exit 1
	fi
fi

if [ ! -d "${TMPDIR}" ] ; then
	TMPDIR=/tmp
fi

if [ ! -d "${RELEASEDIR}" ] ; then
	RELEASEDIR=/releases
fi

# Set the OS and ABI environment variables, so we can handle multiple
# abis/architectures in a neutral way
export OS=$(uname)

case "${OS}" in
	OpenBSD)
		export ABI="$(sysctl -n kern.osrelease)"
		export MACHINE="$(machine)"
		export ARCH="$(machine -a)"
	;;
	Darwin)
		export ABI="$(sysctl -n kern.osrelease | cut -d . -f 1)"
		export MACHINE="$(sysctl -n hw.machine)"
		if [ "${MACHINE}" != "i386" ] ; then
			export MACHINE="powerpc"
		fi
		export ARCH="${MACHINE}"
	;;
	DragonFly)
		export ABI="$(sysctl -n kern.osrelease | cut -d . -f 1,2)"
		export MACHINE="$(sysctl -n hw.machine)"
		export ARCH="$(sysctl -n hw.machine_arch)"
	;;
	*)
		export ABI="$(sysctl -n kern.osrelease | cut -d . -f 1)"
		export MACHINE="$(sysctl -n hw.machine)"
		export ARCH="$(sysctl -n hw.machine_arch)"
		kldload nullfs 2>${DEVICES}/null >${DEVICES}/null
		kldload tmpfs 2>${DEVICES}/null >${DEVICES}/null
		MODEL="$(kenv -q smbios.system.maker)"
		case "${MODEL}" in
			EeePC-1234567890)
				MODEL="EEEPC-$(kenv -q smbios.system.serial)"
			;;
			*)
				MODEL="GENERIC"
			;;
		esac
	;;
esac

usage () {
	cat << EOF
usage: run version [directory]

EOF
	exit 2
}

run () {
	if [ "${1}" = "" ] ; then
		usage
	fi
	if [ -d "${2}" ] ; then
		DESTINATIONDIR=${2}
	else
		DESTINATIONDIR="${TMPDIR}/destdir/$(dd if=/dev/random bs=4m count=1 2>${DEVICES}/null | sha256 -q)"
		mkdir -p ${DESTINATIONDIR}
	fi

	RELEASE=${RELEASEDIR}/evoke/${1}/product/evoke.fs.gz
	if [ "$(file -ib ${RELEASE})" != "application/x-gzip" ] ; then
		exit 3
	fi
	FILE="${TMPDIR}/$(dd if=/dev/random bs=4m count=1 2>${DEVICES}/null | sha256 -q).fs"
	gunzip -c ${RELEASE} >${FILE}
	OPTIONS="readonly" mounter ufs:${FILE} ${DESTINATIONDIR}
	CHROOT=$(command -v chroot)
	BINDIR=/system/${OS}-${ABI}/${ARCH}/bin
	cd ${DESTINATIONDIR}
	if [ ! -f "${DESTINATIONDIR}${BINDIR}/sh" ] ; then
		if [ "${OS}" = "FreeBSD" ] ; then
			export ABI=$(for compatabi in $(jot $(( ${ABI} - 1 )) $(( ${ABI} - 1 )) 2)
			do
				if [ -f "${DESTINATIONDIR}/system/${OS}-${compatabi}/${ARCH}/bin/sh" ] ; then
					echo ${compatabi}
				fi
			done | head -n 1)
		else
			tcsh
			exit 4
		fi
	fi
	cd ${ROOTDIR}
	BINDIR=/system/${OS}-${ABI}/${ARCH}/bin
	mounter devfs: ${DESTINATIONDIR}/dev
	PATH=${BINDIR} ${CHROOT} ${DESTINATIONDIR} ${BINDIR}/sh /system/share/bin/systart
	umount ${DESTINATIONDIR}/mem/curuser
	umount ${DESTINATIONDIR}/config
	umount ${DESTINATIONDIR}/config
	umount ${DESTINATIONDIR}/mem
	umount ${DESTINATIONDIR}/boot
	umount ${DESTINATIONDIR}/bin
	umount ${DESTINATIONDIR}/bin
	umount ${DESTINATIONDIR}/lib
	umount ${DESTINATIONDIR}/libexec
	umount ${DESTINATIONDIR}/dev
	mounter umount ${DESTINATIONDIR}
	rm ${FILE}
	if [ ! -d "${2}" ] ; then
		rm -r ${DESTINATIONDIR}
	fi
}

run $*
